package com.dgshanger.sanhxiaofeisc.mvp.model

import android.text.TextUtils
import com.dgshanger.sanhxiaofeisc.common.AppApi
import com.dgshanger.sanhxiaofeisc.entity.PictureEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.GradeContract
import com.xx.baseutilslibrary.network.entity.BaseResponseEntity
import com.xx.baseutilslibrary.network.rx.RxHelper
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver
import io.reactivex.Observable
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File


/**
 * Created by JacobHHH on 2018/4/20.
 */
class GradeModel : GradeContract.Model {

    override fun submitComment(order_sn: String?, comment_score: String?, comment_content: String?, img: MutableList<String>?, httpObserver: XxBaseHttpObserver<Any>?) {

        val  sb = StringBuffer()
        if (!img!!.isEmpty()){
            val type = RequestBody.create(MultipartBody.FORM, "app_order_comment")
            val imgFile0 = File(img[0])
            val imageBody0 = RequestBody.create(MultipartBody.FORM, imgFile0)
            val image0 = MultipartBody.Part.createFormData("newfile", imgFile0.name, imageBody0)

            AppApi.Api().uploadPicture(type,image0)
                    .flatMap { pictureEntity ->

                        sb.append(pictureEntity.data!!.file_path)
                        if (img.size>1){
                            val imgFile1 = File(img[1])
                            val imageBody1 = RequestBody.create(MultipartBody.FORM, imgFile1)
                            val image1 = MultipartBody.Part.createFormData("newfile", imgFile1.name, imageBody1)
                            AppApi.Api().uploadPicture(type,image1)
                        }else{
                            //没有第二张,直接返回模拟数据
                            Observable.just(BaseResponseEntity<PictureEntity>("1", PictureEntity("")))
                        }

                    }
                    .flatMap { pictureEntity ->
                        if (!TextUtils.isEmpty(pictureEntity.data!!.file_path)){
                            sb.append(pictureEntity.data!!.file_path).append(",")
                            if (img.size>2){
                                val imgFile2 = File(img[2])
                                val imageBody2 = RequestBody.create(MultipartBody.FORM, imgFile2)
                                val image2 = MultipartBody.Part.createFormData("newfile", imgFile2.name, imageBody2)
                                AppApi.Api().uploadPicture(type,image2)
                            }else{
                                //没有第三张,直接返回模拟数据
                                Observable.just(BaseResponseEntity<PictureEntity>("1", PictureEntity("")))
                            }
                        }else{
                            //没有第三张,直接返回模拟数据
                            Observable.just(BaseResponseEntity<PictureEntity>("1", PictureEntity("")))
                        }
                    }
                    .flatMap { pictureEntity->
                        if (!TextUtils.isEmpty(pictureEntity.data!!.file_path)){
                            sb.append(pictureEntity.data!!.file_path)
                        }
                        AppApi.Api().submitComment(order_sn,comment_score,comment_content,sb.toString())
                    }
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)
        }else{
            AppApi.Api().submitComment(order_sn,comment_score,comment_content,null)
                    .compose(RxHelper.io_main())
                    .compose(RxHelper.start_finish(httpObserver))
                    .subscribe(httpObserver)

        }

    }
}